package org.cug.controller;

import lombok.extern.slf4j.Slf4j;
import org.cug.pojo.Result;
import org.cug.pojo.UndergraduateInstruction;
import org.cug.service.UndergraduateInstructionService;
import org.cug.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/undergraduateInstruction")
/**
 * 该控制器用于处理本科授课相关的代码的增删改查，操纵的数据库为undergraduate_instruction
 * state:已测验
 */
public class UndergraduateInstructionController {
    @Autowired
    private UndergraduateInstructionService undergraduateService;

    @GetMapping
    public Result list(@RequestBody(required = false) Short semester){
        log.info("查询所有本科授课信息");
        //本地线程获取
        Map<String,Object> claims= ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        List<UndergraduateInstruction> undergraduate=undergraduateService.list(userId,semester);
        return Result.success(undergraduate);
    }

    @PostMapping
    public Result add(@RequestBody UndergraduateInstruction undergraduate){
        Map<String,Object> claims= ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        undergraduate.setUserId(userId);
        log.info("增加本科授课信息{}",undergraduate);
        undergraduateService.add(undergraduate);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除本科授课信息{}",ids);
        undergraduateService.delete(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectid(@PathVariable Integer id){
        log.info("查询id{}",id);
        UndergraduateInstruction undergraduate = undergraduateService.selectid(id);
        return Result.success(undergraduate);
    }

    @PutMapping
    public Result alterbyid(@RequestBody UndergraduateInstruction undergraduate){
        log.info("修改本科授课信息{}",undergraduate);
        undergraduateService.alterbyid(undergraduate);
        return Result.success();
    }
}
